package cn.rulian.base.basis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rulian.base.basis.bo.SortManager;
import cn.rulian.base.basis.service.SortManagerService;
import cn.rulian.base.basis.vo.SortManagerVo;
import cn.rulian.base.common.dto.Message;
import cn.rulian.base.common.vo.Result;

/**
 * 分类管理器 Controller
 */

@Controller
@RequestMapping("/base/basis/sortManager")
public class SortManagerController {
	
	@Autowired
	private SortManagerService service;
	
	private final String path="base/basis/sortManager_"; //页面基本路径

	/**
	 * 页面基本路径
	 */
	private String getPath()
	{
		return path;
	}
	
	/**
	 * 列表页面
	 */
	@RequestMapping("/listPage")
	public String listPage() {
		return this.getPath()+"list";
	}

	/**
	 * 查询
	 */
	@RequestMapping("/listData")
	@ResponseBody
	public Result listData(SortManagerVo vo) {
		return service.query(vo);
	}

	/**
	 * 增加页面
	 */
	@RequestMapping("/addPage")
	public String addPage() {
		return this.getPath()+"add";
	}

	/**
	 * 增加
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Message add(SortManagerVo vo) {
		Message rm = new Message();
		SortManager bo = vo.getObj();
		
		//key判重
		SortManager other = service.findFirstByKey(bo.getKey());
		if(other != null)
		{
			rm.setMsg("key重复，请修改！");
			rm.setCode(Message._ERROR);
			return rm;
		}
		
		try {
			service.save(bo);
			rm.setMsg("保存成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("保存失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改页面
	 */
	@RequestMapping("/updatePage")
	public String updatePage(Model model, Long id) {
		model.addAttribute("bo", service.findOne(id));
		return this.getPath()+"update";
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Message update(SortManagerVo vo,Long id) {
		Message rm = new Message();
		try {
			SortManager obj = vo.getObj();

			//key判重
			SortManager other = service.findFirstByKey(obj.getKey());
			if(other != null && other.getId().longValue() != id.longValue())
			{
				rm.setMsg("key重复，请修改！");
				rm.setCode(Message._ERROR);
				return rm;
			}
			
			SortManager bo = service.findOne(id);
			bo.setKey(obj.getKey());
			bo.setName(obj.getName());
			bo.setBoName(obj.getBoName());
			bo.setDaoName(obj.getDaoName());
			bo.setRemark(obj.getRemark());
			bo.setOrderNum(obj.getOrderNum());
			service.save(bo);
			
			rm.setMsg("保存成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("保存失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Message delete(Long ids[]) {
		Message rm = new Message();
		try {
			for(Long id:ids)
			{
				service.delete(id);
			}
			
			rm.setMsg("删除成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("删除失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 详情页面
	 */
	@RequestMapping("/detailPage")
	public String detailPage(Model model, Long id) {
		model.addAttribute("bo", service.findOne(id));
		return this.getPath()+"detail";
	}

	
	
}
